<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>vec2.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_vec_vec2.js.html">vec2.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Definition of 2 element vectors.  This follows the same design
<a name="line17"></a> * patterns as Vec3 and Vec4.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.vec.Vec2&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.vec&#39;);
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>/** @typedef {goog.vec.Float32} */ goog.vec.Vec2.Float32;
<a name="line27"></a>/** @typedef {goog.vec.Float64} */ goog.vec.Vec2.Float64;
<a name="line28"></a>/** @typedef {goog.vec.Number} */ goog.vec.Vec2.Number;
<a name="line29"></a>/** @typedef {goog.vec.AnyType} */ goog.vec.Vec2.AnyType;
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>/**
<a name="line33"></a> * Creates a 2 element vector of Float32. The array is initialized to zero.
<a name="line34"></a> *
<a name="line35"></a> * @return {!goog.vec.Vec2.Float32} The new 2 element array.
<a name="line36"></a> */
<a name="line37"></a>goog.vec.Vec2.createFloat32 = function() {
<a name="line38"></a>  return new Float32Array(2);
<a name="line39"></a>};
<a name="line40"></a>
<a name="line41"></a>
<a name="line42"></a>/**
<a name="line43"></a> * Creates a 2 element vector of Float64. The array is initialized to zero.
<a name="line44"></a> *
<a name="line45"></a> * @return {!goog.vec.Vec2.Float64} The new 2 element array.
<a name="line46"></a> */
<a name="line47"></a>goog.vec.Vec2.createFloat64 = function() {
<a name="line48"></a>  return new Float64Array(2);
<a name="line49"></a>};
<a name="line50"></a>
<a name="line51"></a>
<a name="line52"></a>/**
<a name="line53"></a> * Creates a 2 element vector of Number. The array is initialized to zero.
<a name="line54"></a> *
<a name="line55"></a> * @return {!goog.vec.Vec2.Number} The new 2 element array.
<a name="line56"></a> */
<a name="line57"></a>goog.vec.Vec2.createNumber = function() {
<a name="line58"></a>  var a = new Array(2);
<a name="line59"></a>  goog.vec.Vec2.setFromValues(a, 0, 0);
<a name="line60"></a>  return a;
<a name="line61"></a>};
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>/**
<a name="line65"></a> * Creates a new 2 element FLoat32 vector initialized with the value from the
<a name="line66"></a> * given array.
<a name="line67"></a> *
<a name="line68"></a> * @param {goog.vec.Vec2.AnyType} vec The source 2 element array.
<a name="line69"></a> * @return {!goog.vec.Vec2.Float32} The new 2 element array.
<a name="line70"></a> */
<a name="line71"></a>goog.vec.Vec2.createFloat32FromArray = function(vec) {
<a name="line72"></a>  var newVec = goog.vec.Vec2.createFloat32();
<a name="line73"></a>  goog.vec.Vec2.setFromArray(newVec, vec);
<a name="line74"></a>  return newVec;
<a name="line75"></a>};
<a name="line76"></a>
<a name="line77"></a>
<a name="line78"></a>/**
<a name="line79"></a> * Creates a new 2 element Float32 vector initialized with the supplied values.
<a name="line80"></a> *
<a name="line81"></a> * @param {number} vec0 The value for element at index 0.
<a name="line82"></a> * @param {number} vec1 The value for element at index 1.
<a name="line83"></a> * @return {!goog.vec.Vec2.Float32} The new vector.
<a name="line84"></a> */
<a name="line85"></a>goog.vec.Vec2.createFloat32FromValues = function(vec0, vec1) {
<a name="line86"></a>  var a = goog.vec.Vec2.createFloat32();
<a name="line87"></a>  goog.vec.Vec2.setFromValues(a, vec0, vec1);
<a name="line88"></a>  return a;
<a name="line89"></a>};
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>/**
<a name="line93"></a> * Creates a clone of the given 2 element Float32 vector.
<a name="line94"></a> *
<a name="line95"></a> * @param {goog.vec.Vec2.Float32} vec The source 2 element vector.
<a name="line96"></a> * @return {!goog.vec.Vec2.Float32} The new cloned vector.
<a name="line97"></a> */
<a name="line98"></a>goog.vec.Vec2.cloneFloat32 = goog.vec.Vec2.createFloat32FromArray;
<a name="line99"></a>
<a name="line100"></a>
<a name="line101"></a>/**
<a name="line102"></a> * Creates a new 2 element Float64 vector initialized with the value from the
<a name="line103"></a> * given array.
<a name="line104"></a> *
<a name="line105"></a> * @param {goog.vec.Vec2.AnyType} vec The source 2 element array.
<a name="line106"></a> * @return {!goog.vec.Vec2.Float64} The new 2 element array.
<a name="line107"></a> */
<a name="line108"></a>goog.vec.Vec2.createFloat64FromArray = function(vec) {
<a name="line109"></a>  var newVec = goog.vec.Vec2.createFloat64();
<a name="line110"></a>  goog.vec.Vec2.setFromArray(newVec, vec);
<a name="line111"></a>  return newVec;
<a name="line112"></a>};
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a>* Creates a new 2 element Float64 vector initialized with the supplied values.
<a name="line117"></a>*
<a name="line118"></a>* @param {number} vec0 The value for element at index 0.
<a name="line119"></a>* @param {number} vec1 The value for element at index 1.
<a name="line120"></a>* @return {!goog.vec.Vec2.Float64} The new vector.
<a name="line121"></a>*/
<a name="line122"></a>goog.vec.Vec2.createFloat64FromValues = function(vec0, vec1) {
<a name="line123"></a>  var vec = goog.vec.Vec2.createFloat64();
<a name="line124"></a>  goog.vec.Vec2.setFromValues(vec, vec0, vec1);
<a name="line125"></a>  return vec;
<a name="line126"></a>};
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>/**
<a name="line130"></a> * Creates a clone of the given 2 element vector.
<a name="line131"></a> *
<a name="line132"></a> * @param {goog.vec.Vec2.Float64} vec The source 2 element vector.
<a name="line133"></a> * @return {!goog.vec.Vec2.Float64} The new cloned vector.
<a name="line134"></a> */
<a name="line135"></a>goog.vec.Vec2.cloneFloat64 = goog.vec.Vec2.createFloat64FromArray;
<a name="line136"></a>
<a name="line137"></a>
<a name="line138"></a>/**
<a name="line139"></a> * Initializes the vector with the given values.
<a name="line140"></a> *
<a name="line141"></a> * @param {goog.vec.Vec2.AnyType} vec The vector to receive the values.
<a name="line142"></a> * @param {number} vec0 The value for element at index 0.
<a name="line143"></a> * @param {number} vec1 The value for element at index 1.
<a name="line144"></a> * @return {!goog.vec.Vec2.AnyType} Return vec so that operations can be
<a name="line145"></a> *     chained together.
<a name="line146"></a> */
<a name="line147"></a>goog.vec.Vec2.setFromValues = function(vec, vec0, vec1) {
<a name="line148"></a>  vec[0] = vec0;
<a name="line149"></a>  vec[1] = vec1;
<a name="line150"></a>  return vec;
<a name="line151"></a>};
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>/**
<a name="line155"></a> * Initializes the vector with the given array of values.
<a name="line156"></a> *
<a name="line157"></a> * @param {goog.vec.Vec2.AnyType} vec The vector to receive the
<a name="line158"></a> *     values.
<a name="line159"></a> * @param {goog.vec.Vec2.AnyType} values The array of values.
<a name="line160"></a> * @return {!goog.vec.Vec2.AnyType} Return vec so that operations can be
<a name="line161"></a> *     chained together.
<a name="line162"></a> */
<a name="line163"></a>goog.vec.Vec2.setFromArray = function(vec, values) {
<a name="line164"></a>  vec[0] = values[0];
<a name="line165"></a>  vec[1] = values[1];
<a name="line166"></a>  return vec;
<a name="line167"></a>};
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>/**
<a name="line171"></a> * Performs a component-wise addition of vec0 and vec1 together storing the
<a name="line172"></a> * result into resultVec.
<a name="line173"></a> *
<a name="line174"></a> * @param {goog.vec.Vec2.AnyType} vec0 The first addend.
<a name="line175"></a> * @param {goog.vec.Vec2.AnyType} vec1 The second addend.
<a name="line176"></a> * @param {goog.vec.Vec2.AnyType} resultVec The vector to
<a name="line177"></a> *     receive the result. May be vec0 or vec1.
<a name="line178"></a> * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
<a name="line179"></a> *     chained together.
<a name="line180"></a> */
<a name="line181"></a>goog.vec.Vec2.add = function(vec0, vec1, resultVec) {
<a name="line182"></a>  resultVec[0] = vec0[0] + vec1[0];
<a name="line183"></a>  resultVec[1] = vec0[1] + vec1[1];
<a name="line184"></a>  return resultVec;
<a name="line185"></a>};
<a name="line186"></a>
<a name="line187"></a>
<a name="line188"></a>/**
<a name="line189"></a> * Performs a component-wise subtraction of vec1 from vec0 storing the
<a name="line190"></a> * result into resultVec.
<a name="line191"></a> *
<a name="line192"></a> * @param {goog.vec.Vec2.AnyType} vec0 The minuend.
<a name="line193"></a> * @param {goog.vec.Vec2.AnyType} vec1 The subtrahend.
<a name="line194"></a> * @param {goog.vec.Vec2.AnyType} resultVec The vector to
<a name="line195"></a> *     receive the result. May be vec0 or vec1.
<a name="line196"></a> * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
<a name="line197"></a> *     chained together.
<a name="line198"></a> */
<a name="line199"></a>goog.vec.Vec2.subtract = function(vec0, vec1, resultVec) {
<a name="line200"></a>  resultVec[0] = vec0[0] - vec1[0];
<a name="line201"></a>  resultVec[1] = vec0[1] - vec1[1];
<a name="line202"></a>  return resultVec;
<a name="line203"></a>};
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>/**
<a name="line207"></a> * Negates vec0, storing the result into resultVec.
<a name="line208"></a> *
<a name="line209"></a> * @param {goog.vec.Vec2.AnyType} vec0 The vector to negate.
<a name="line210"></a> * @param {goog.vec.Vec2.AnyType} resultVec The vector to
<a name="line211"></a> *     receive the result. May be vec0.
<a name="line212"></a> * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
<a name="line213"></a> *     chained together.
<a name="line214"></a> */
<a name="line215"></a>goog.vec.Vec2.negate = function(vec0, resultVec) {
<a name="line216"></a>  resultVec[0] = -vec0[0];
<a name="line217"></a>  resultVec[1] = -vec0[1];
<a name="line218"></a>  return resultVec;
<a name="line219"></a>};
<a name="line220"></a>
<a name="line221"></a>
<a name="line222"></a>/**
<a name="line223"></a> * Multiplies each component of vec0 with scalar storing the product into
<a name="line224"></a> * resultVec.
<a name="line225"></a> *
<a name="line226"></a> * @param {goog.vec.Vec2.AnyType} vec0 The source vector.
<a name="line227"></a> * @param {number} scalar The value to multiply with each component of vec0.
<a name="line228"></a> * @param {goog.vec.Vec2.AnyType} resultVec The vector to
<a name="line229"></a> *     receive the result. May be vec0.
<a name="line230"></a> * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
<a name="line231"></a> *     chained together.
<a name="line232"></a> */
<a name="line233"></a>goog.vec.Vec2.scale = function(vec0, scalar, resultVec) {
<a name="line234"></a>  resultVec[0] = vec0[0] * scalar;
<a name="line235"></a>  resultVec[1] = vec0[1] * scalar;
<a name="line236"></a>  return resultVec;
<a name="line237"></a>};
<a name="line238"></a>
<a name="line239"></a>
<a name="line240"></a>/**
<a name="line241"></a> * Returns the magnitudeSquared of the given vector.
<a name="line242"></a> *
<a name="line243"></a> * @param {goog.vec.Vec2.AnyType} vec0 The vector.
<a name="line244"></a> * @return {number} The magnitude of the vector.
<a name="line245"></a> */
<a name="line246"></a>goog.vec.Vec2.magnitudeSquared = function(vec0) {
<a name="line247"></a>  var x = vec0[0], y = vec0[1];
<a name="line248"></a>  return x * x + y * y;
<a name="line249"></a>};
<a name="line250"></a>
<a name="line251"></a>
<a name="line252"></a>/**
<a name="line253"></a> * Returns the magnitude of the given vector.
<a name="line254"></a> *
<a name="line255"></a> * @param {goog.vec.Vec2.AnyType} vec0 The vector.
<a name="line256"></a> * @return {number} The magnitude of the vector.
<a name="line257"></a> */
<a name="line258"></a>goog.vec.Vec2.magnitude = function(vec0) {
<a name="line259"></a>  var x = vec0[0], y = vec0[1];
<a name="line260"></a>  return Math.sqrt(x * x + y * y);
<a name="line261"></a>};
<a name="line262"></a>
<a name="line263"></a>
<a name="line264"></a>/**
<a name="line265"></a> * Normalizes the given vector storing the result into resultVec.
<a name="line266"></a> *
<a name="line267"></a> * @param {goog.vec.Vec2.AnyType} vec0 The vector to normalize.
<a name="line268"></a> * @param {goog.vec.Vec2.AnyType} resultVec The vector to
<a name="line269"></a> *     receive the result. May be vec0.
<a name="line270"></a> * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
<a name="line271"></a> *     chained together.
<a name="line272"></a> */
<a name="line273"></a>goog.vec.Vec2.normalize = function(vec0, resultVec) {
<a name="line274"></a>  var ilen = 1 / goog.vec.Vec2.magnitude(vec0);
<a name="line275"></a>  resultVec[0] = vec0[0] * ilen;
<a name="line276"></a>  resultVec[1] = vec0[1] * ilen;
<a name="line277"></a>  return resultVec;
<a name="line278"></a>};
<a name="line279"></a>
<a name="line280"></a>
<a name="line281"></a>/**
<a name="line282"></a> * Returns the scalar product of vectors vec0 and vec1.
<a name="line283"></a> *
<a name="line284"></a> * @param {goog.vec.Vec2.AnyType} vec0 The first vector.
<a name="line285"></a> * @param {goog.vec.Vec2.AnyType} vec1 The second vector.
<a name="line286"></a> * @return {number} The scalar product.
<a name="line287"></a> */
<a name="line288"></a>goog.vec.Vec2.dot = function(vec0, vec1) {
<a name="line289"></a>  return vec0[0] * vec1[0] + vec0[1] * vec1[1];
<a name="line290"></a>};
<a name="line291"></a>
<a name="line292"></a>
<a name="line293"></a>/**
<a name="line294"></a> * Returns the squared distance between two points.
<a name="line295"></a> *
<a name="line296"></a> * @param {goog.vec.Vec2.AnyType} vec0 First point.
<a name="line297"></a> * @param {goog.vec.Vec2.AnyType} vec1 Second point.
<a name="line298"></a> * @return {number} The squared distance between the points.
<a name="line299"></a> */
<a name="line300"></a>goog.vec.Vec2.distanceSquared = function(vec0, vec1) {
<a name="line301"></a>  var x = vec0[0] - vec1[0];
<a name="line302"></a>  var y = vec0[1] - vec1[1];
<a name="line303"></a>  return x * x + y * y;
<a name="line304"></a>};
<a name="line305"></a>
<a name="line306"></a>
<a name="line307"></a>/**
<a name="line308"></a> * Returns the distance between two points.
<a name="line309"></a> *
<a name="line310"></a> * @param {goog.vec.Vec2.AnyType} vec0 First point.
<a name="line311"></a> * @param {goog.vec.Vec2.AnyType} vec1 Second point.
<a name="line312"></a> * @return {number} The distance between the points.
<a name="line313"></a> */
<a name="line314"></a>goog.vec.Vec2.distance = function(vec0, vec1) {
<a name="line315"></a>  return Math.sqrt(goog.vec.Vec2.distanceSquared(vec0, vec1));
<a name="line316"></a>};
<a name="line317"></a>
<a name="line318"></a>
<a name="line319"></a>/**
<a name="line320"></a> * Returns a unit vector pointing from one point to another.
<a name="line321"></a> * If the input points are equal then the result will be all zeros.
<a name="line322"></a> *
<a name="line323"></a> * @param {goog.vec.Vec2.AnyType} vec0 Origin point.
<a name="line324"></a> * @param {goog.vec.Vec2.AnyType} vec1 Target point.
<a name="line325"></a> * @param {goog.vec.Vec2.AnyType} resultVec The vector to receive the
<a name="line326"></a> *     results (may be vec0 or vec1).
<a name="line327"></a> * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
<a name="line328"></a> *     chained together.
<a name="line329"></a> */
<a name="line330"></a>goog.vec.Vec2.direction = function(vec0, vec1, resultVec) {
<a name="line331"></a>  var x = vec1[0] - vec0[0];
<a name="line332"></a>  var y = vec1[1] - vec0[1];
<a name="line333"></a>  var d = Math.sqrt(x * x + y * y);
<a name="line334"></a>  if (d) {
<a name="line335"></a>    d = 1 / d;
<a name="line336"></a>    resultVec[0] = x * d;
<a name="line337"></a>    resultVec[1] = y * d;
<a name="line338"></a>  } else {
<a name="line339"></a>    resultVec[0] = resultVec[1] = 0;
<a name="line340"></a>  }
<a name="line341"></a>  return resultVec;
<a name="line342"></a>};
<a name="line343"></a>
<a name="line344"></a>
<a name="line345"></a>/**
<a name="line346"></a> * Linearly interpolate from vec0 to vec1 according to f. The value of f should
<a name="line347"></a> * be in the range [0..1] otherwise the results are undefined.
<a name="line348"></a> *
<a name="line349"></a> * @param {goog.vec.Vec2.AnyType} vec0 The first vector.
<a name="line350"></a> * @param {goog.vec.Vec2.AnyType} vec1 The second vector.
<a name="line351"></a> * @param {number} f The interpolation factor.
<a name="line352"></a> * @param {goog.vec.Vec2.AnyType} resultVec The vector to receive the
<a name="line353"></a> *     results (may be vec0 or vec1).
<a name="line354"></a> * @return {!goog.vec.Vec2.AnyType} Return resultVec so that operations can be
<a name="line355"></a> *     chained together.
<a name="line356"></a> */
<a name="line357"></a>goog.vec.Vec2.lerp = function(vec0, vec1, f, resultVec) {
<a name="line358"></a>  var x = vec0[0], y = vec0[1];
<a name="line359"></a>  resultVec[0] = (vec1[0] - x) * f + x;
<a name="line360"></a>  resultVec[1] = (vec1[1] - y) * f + y;
<a name="line361"></a>  return resultVec;
<a name="line362"></a>};
<a name="line363"></a>
<a name="line364"></a>
<a name="line365"></a>/**
<a name="line366"></a> * Returns true if the components of vec0 are equal to the components of vec1.
<a name="line367"></a> *
<a name="line368"></a> * @param {goog.vec.Vec2.AnyType} vec0 The first vector.
<a name="line369"></a> * @param {goog.vec.Vec2.AnyType} vec1 The second vector.
<a name="line370"></a> * @return {boolean} True if the vectors are equal, false otherwise.
<a name="line371"></a> */
<a name="line372"></a>goog.vec.Vec2.equals = function(vec0, vec1) {
<a name="line373"></a>  return vec0.length == vec1.length &amp;&amp;
<a name="line374"></a>      vec0[0] == vec1[0] &amp;&amp; vec0[1] == vec1[1];
<a name="line375"></a>};
</pre>


</body>
</html>
